package com.axend.aerosense.room.ui.fragment.vipfun;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.axend.aerosense.common.ui.BottomTextPickerDialog;
import com.axend.aerosense.room.entity.RoomRemindBean;
import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.axend.aerosense.room.entity.VipFunSaveDataBean;
import com.axend.aerosense.room.ui.fragment.BottomNumberPickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentStandingStillBinding;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.Map;

/**
 * 原地静止
 * Author: ChenHongWei
 * Date: 2021/3/23 11:00
 */
public class RoomStandingStillFragment extends BaseVipFunFragment<RoomFragmentStandingStillBinding> {

    private Map<String, Float> timeMap;
    private int selectAlarmValue;
    private int selectRemindValue;
    private String[] timeList;

    public static RoomStandingStillFragment getInstance(){
        return new RoomStandingStillFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_standing_still;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onChange(CompoundButton buttonView, boolean isChecked) {
        super.onChange(buttonView, isChecked);
    }

    @Override
    protected void saveSettings() {
        super.saveSettings();
        VipFunSaveDataBean bean = new VipFunSaveDataBean(
                getRoomUuid(),
                getRoomType(),
                getCurrentActivity().getSwitchState(),
                selectRemindValue,
                selectAlarmValue);
        viewModel.saveData(bean, UrlApi.URL.SetPositionStatic.getUrl());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getCurrentActivity().setVipFunTip(R.string.room_standing_still_tips);
        setTitle(R.string.room_motionless_alert);

        init();
    }

    private void init(){
        timeMap = getTimeList(0.5f, 12f, 0.5f, getUnit());
        timeList = timeMap.keySet().toArray(new String[timeMap.size()]);

        viewDataBinding.roomAlarmConstraintLayout.setOnClickListener(v -> {
//            BottomNumberPickerDialog fragmentDialog = new BottomNumberPickerDialog.Builder()
//                    .setType(Type.NUMBER)
//                    .setMinValue(30)  //最小3
//                    .setMaxValue(120)  //最大
//                    .setDefValue(60)  //默认
//                    .setUnit(getUnit())  //单位
//                    .setWheelItemTextSize(16)
//                    .setStep(15)  //步长
//                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
//                    .setCallBack((timePickerView, value) -> {
//                        viewDataBinding.roomAlarmTime.setText(value +" "+ getString(R.string.room_minute));
//                        change();
//                    })
//                    .build();
            BottomTextPickerDialog fragment = new BottomTextPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setWheelItemTextSize(16)
                    .setDefValue(3)  //默认值（数组下标）
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setIndexCallBack(new OnDateSetListener<Integer>() {
                        @Override
                        public void onDateSet(Fragment timePickerView, Integer sex) {
                            viewDataBinding.roomAlarmTime.setText(timeList[sex]);
                            selectAlarmValue = (int)(timeMap.get(timeList[sex]) * 60);
                            change();
                        }
                    })
                    .build(timeList);
            fragment.show(getChildFragmentManager(), BottomTextPickerDialog.class.getSimpleName());
        });

        viewDataBinding.roomWarningConstraintLayout.setOnClickListener(v -> {
            BottomTextPickerDialog fragment = new BottomTextPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setWheelItemTextSize(16)
                    .setDefValue(3)  //默认值（数组下标）
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setIndexCallBack(new OnDateSetListener<Integer>() {
                        @Override
                        public void onDateSet(Fragment timePickerView, Integer sex) {
                            viewDataBinding.roomWarningTime.setText(timeList[sex]);
                            selectRemindValue = (int)(timeMap.get(timeList[sex]) * 60);
                            change();
                        }
                    })
                    .build(timeList);
            fragment.show(getChildFragmentManager(), BottomTextPickerDialog.class.getSimpleName());
        });

        RoomVipSetBean vipSettings = getVipSettings();
        if (vipSettings != null){
            getCurrentActivity().setSwitchChange(vipSettings.getFunctionSwitch() == 1);
            selectAlarmValue = vipSettings.getFunctionAlert();
            selectRemindValue = vipSettings.getFunctionRemind();
            viewDataBinding.roomAlarmTime.setText((vipSettings.getFunctionAlert() / 60f)+" " + getUnit());
            viewDataBinding.roomWarningTime.setText((vipSettings.getFunctionRemind() / 60f)+" " + getUnit());
        }
    }

    private String getUnit(){
        return getString(R.string.room_hour);
    }
}
